<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/16 0016
 * Time: 17:27
 */
namespace app\admin\controller;
use app\models\Models;

class Dynamic extends Check{
    /**
     * 动态列表
     * @return \think\response\View
     */
    public function dynamicList(){
        $get = input('param.');
        $dynamic = new Models('dynamic');
        // 条件查询
        $search = search($get,'a.uid,b.nickname','a.create_time','a.status');
        $where  = $search['condition'];
        $join   = [
            ['user b','a.uid = b.id','left']
        ];
        $field  = "a.*,b.nickname";
        $list = $dynamic->_page($where,$field,'a.id desc',$join,$get['page'],6,$get);
        $config = self::getConfig();
        $pre = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'dynamic'. DS . 's';
        return view('dynamicList',['list'=>$list->items(),'page'=>$list->render(),'display'=>$search['display'],'pre'=>$pre]);
    }

    /**
     * 动态详情
     * @return \think\response\View
     */
    public function dynamicShow(){
        $get = input('param.');
        $dynamic = new Models('dynamic');
        $join = [
            ['user b','a.uid = b.id','left'],
        ];
        $field = 'a.*,b.portrait';
        $info  = $dynamic->joinOne($join,['a.id'=>$get['id']],$field);
        $config = self::getConfig();
        $pre = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'dynamic'. DS;
        return view('dynamicShow',['info'=>$info,'pre'=>$pre]);
    }

    /**
     * 启用
     * @return array
     */
    public function start(){
        if(request()->isAjax()){
            $post = input('param.');
            $user = new Models('dynamic');
            try{
                $bool = $user->setValue(['id'=>$post['id']],['status'=>$user::STATUS_ON]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 停用
     * @return array
     */
    public function stop(){
        if(request()->isAjax()){
            $post = input('param.');
            $user = new Models('dynamic');
            try{
                $bool = $user->setValue(['id'=>$post['id']],['status'=>$user::STATUS_OFF]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }
}